package com.webmagic.demo.Instrument;

import com.alibaba.fastjson.JSON;
import com.webmagic.demo.mv.MoveDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @description
 * @Auther zhanglu
 * @Date 2018/4/17 上午1:26
 */
@Slf4j
@Service
public class SpiderService implements Pipeline {
    private static List<Instrument> list = Collections.synchronizedList(new ArrayList<>());
    @Value("${file.nameBegin:1}")
    private int fileName;
    @Autowired
    private FileConfig fileConfig;
    @Autowired
    private MvProcessor mvProcessor;

    @Override
    public void process(ResultItems resultItems, Task task) {
        Object object = resultItems.get("bean");
        if (object != null) {
//            if (list.size() >= fileConfig.getSize()) {
//                if (createJsonFile(JSON.toJSON(list).toString())) {
//                    list.clear();
//                    log.info("达到文件内数据最大数量，创建文件成功, 清除list数据,{}", list);
//                }
//            }

            Instrument instrument = (Instrument) object;
            log.debug("C{}: {}", mvProcessor.getNum() - 1, JSON.toJSON(instrument));
            if (StringUtils.isNotBlank(instrument.getChineseName())) {
                if (createJsonFile(JSON.toJSON(instrument).toString())) {
                    log.info("创建文件成功, C{}", mvProcessor.getNum() - 1);
                }
            } else {
                log.warn("C{}无数据, 最终结束C{}", mvProcessor.getNum() - 1, fileConfig.getEnd());
            }
        }
    }

    public boolean createJsonFile(String jsonString) {
        // 标记文件生成是否成功
        boolean flag = true;

        // 拼接文件完整路径
        String fullPath = fileConfig.getPath() + fileName + ".json";

        // 生成json格式文件
        try {
            // 保证创建一个新文件
            File file = new File(fullPath);
            if (!file.getParentFile().exists()) { // 如果父目录不存在，创建父目录
                file.getParentFile().mkdirs();
            }
            if (file.exists()) { // 如果已存在,删除旧文件
                file.delete();
            }
            file.createNewFile();

            // 将格式化后的字符串写入文件
            Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            write.write(jsonString);
            write.flush();
            write.close();
        } catch (Exception e) {
            flag = false;
            e.printStackTrace();
        }

        // 返回是否成功的标记
        fileName++;
        return flag;
    }

}
